<template>
  <el-container>
    <el-main>
      <el-container>
        <el-main>
          <el-form ref="elForm" :model="formData"  size="medium" label-width="100px" style="width:320px;margin: 0 auto;">
              <el-form-item v-for="(item, index) in param" :key="index" :label="item.name" :prop="item.code">
                <el-input v-if="item.type=='string'" v-model="item.defaultVal" placeholder="请输入" clearable :style="{width: '220px'}"/>
                <el-input v-if="item.type=='number'" type="number" v-model="item.defaultVal" placeholder="请输入" clearable :style="{width: '220px'}"/>
                <el-date-picker
                  v-if="item.type=='date'"
                  v-model="item.defaultVal"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
                <el-date-picker
                  v-if="item.type=='datetime'"
                  v-model="item.defaultVal"
                  type="datetime"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间">
                </el-date-picker>
              </el-form-item>
          </el-form>
          <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column v-for="(item,index) in cloumns" 
              :key="index"
              :prop="item.name"
              :label="item.title" 
              >
              <template slot-scope="scope">
                <span>{{scope.row[item.name]}}</span>
              </template>
              </el-table-column>
          </el-table>
        </el-main>
      </el-container>
    </el-main>
    <el-footer>
      <div style="float:right;">
      <el-button type="primary" @click="last">上一步</el-button>
      <el-button type="primary" @click="next">完成</el-button>
      <el-button @click="resetForm">取消</el-button>
      </div>
    </el-footer>
  </el-container>
</template>
<script>
  export default {
    props:{
      stepNumber:Object
    },
    data() {
      return {
        formData:{
        },
        param:[],
        tableData:[{
          column2:'啥呀',
          column3:'啥呀',
        },
        {
          column2:'啥呀',
          column3:'啥呀',
        }],
        cloumns:[],
        checkedCloumns:[],
      };
    },
    methods: {
      isShow(item){
        debugger;
        let a = this.checkedCloumns.indexOf(item.id)>=0;
        return a;
      },
      handleClick(tab, event) {
        console.log(tab, event);
      },
      getParam(){
        let _this = this;
        this.axios
        .post(this.HOST+"/apiParameter/tree",{
          apiId:_this.stepNumber.dataApi.id
        })
        .then(function(res) {
          _this.param = res.data.data;
        })
        .catch(function(err) {
          console.log(err);
        });
      },
      last(){
        this.stepNumber.active--;
      },
      next(){
        this.$router.push("/");
      },
      resetForm() {
        this.$router.push("/");
      },
      selectResultByApiId(){
      var _this = this;
      this.axios
        .post(this.HOST+"/apiResult/findByApiId", {
          apiId: this.stepNumber.dataApi.id
        })
        .then(function(res) {
          debugger;
          if(res.data.success && res.data.data.length >0){
            for(let i = 0 ; i < res.data.data.length ; i++){
              _this.checkedCloumns.push(res.data.data[i].cloumnId);
            }
            _this.selectTableByApiId();
          }
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    selectTableByApiId(){
      var _this = this;
      this.axios
        .post(this.HOST+"/apiTable/findByApiId", {
          apiId: this.stepNumber.dataApi.id
        })
        .then(function(res) {
          debugger;
          if(res.data.success ){
            _this.selectResultCloumns(res.data.data.id)
          }
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    selectResultCloumns(tableId){
      var _this = this;
      this.axios
        .post(this.HOST+"/api/manager/modelcolumn/getColumnInfoData", {
          tableId: tableId
        })
        .then(function(res) {
          debugger;
          res = res.data;
          if(res.success ){
            _this.cloumns = [];
            for(let i = 0 ; i < res.data.length ; i++){
              if(_this.checkedCloumns.indexOf(res.data[i].id) >=0){
                 _this.cloumns.push(res.data[i]);
              }
            }
          }
        })
        .catch(function(err) {
          console.log(err);
        });
    },
    },
    mounted(){
      this.getParam();
      this.selectResultByApiId();
    }
  };
</script>